package com.zero.controller;

import com.zero.entity.Article;
import com.zero.service.ArticleService;
import com.zero.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 分页查询最新收录资源
     *
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping("/list/{page}")
    public ModelAndView list(@PathVariable(value = "page", required = false) Integer page) throws Exception {
        ModelAndView mav = new ModelAndView();
        List<Article> articleList = articleService.list(page, 20, Sort.Direction.DESC, "includeDate");
        Long count = articleService.getCount();
        mav.addObject("articleList", articleList);
        mav.addObject("pageCode", PageUtil.genPagination("/article/list", count, page, 20));
        mav.setViewName("newest");
        mav.addObject("modelName", "最新资源收录列表");
        mav.addObject("title", "最新资源收录列表");
        return mav;
    }


    /**
     * 功能描述:根据Id查询详情
     *
     * @param:
     * @return:
     * @auther: 某人的目光
     * @date: 2019/3/3 12:16
     */
    @RequestMapping("/{id}")
    public ModelAndView view(@PathVariable("id") Integer id) throws Exception {
        ModelAndView mav = new ModelAndView();
        Article article = articleService.get(id);
        mav.setViewName("view");
        mav.addObject("article", article);
        mav.addObject("modelName", article.getName());
        mav.addObject("title", article.getName());
        return mav;
    }
}
